/*package com.kyhd.dsp.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kyhd.dsp.config.AppConfig;
import com.kyhd.dsp.controller.model.LoginRequest;
import com.kyhd.dsp.dao.model.User;
import com.kyhd.dsp.service.UserService;
import com.kyhd.dsp.util.Constant;
import com.kyhd.dsp.util.EncryptUtil;
import com.kyhd.lib.util.Constants;
import com.kyhd.lib.util.CookieUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@Api("用户管理接口")
@RestController
@Slf4j
@RequestMapping(value = "/dsp/user", produces = {"application/json" })
public class UserController {

	@Autowired
	UserService userService;
	
	@Autowired
	AppConfig appconfig;
	
	@ApiOperation(value="用户登陆", notes="")
	@RequestMapping(method={RequestMethod.POST},path="/login")
	public @ResponseBody ResponseEntity<String> login(HttpServletResponse response, @RequestBody LoginRequest request) {
		log.info("user login resquest:{}",request);
		HttpHeaders headers = new HttpHeaders();
		headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json;charset=UTF-8"));
		JSONObject ret = new JSONObject();

		String name = request.getName().trim();
		String pwd = request.getPwd().trim();
		pwd = EncryptUtil.encrypt(pwd);
		if(name!=null&&pwd!=null&&!"".equals(name)&&!"".equals(pwd)) {
			User user = this.userService.login(name,pwd);
			if(user!=null) {
				if(user.getUserType()==Constant.UserType.广告主.getType()
						&&user.getReviewState()!=Constant.ReviewState.审核通过.getType()) {
					ret.put("status", Constant.API_STATUS_FAILED);
					ret.put("message", "资质尚未过审，请联系管理员");
					log.info("user login response:{}",ret.toJSONString());
					return new ResponseEntity<String>(ret.toJSONString(), headers, HttpStatus.OK);
				}
				if(!user.getUsable()) {
					ret.put("status", Constant.API_STATUS_FAILED);
					ret.put("message", "您的账号暂时无法登录，请联系管理员");
					log.info("user login response:{}",ret.toJSONString());
					return new ResponseEntity<String>(ret.toJSONString(), headers, HttpStatus.OK);
				}
				
				// 设置客户端 cookie
				CookieUtil.addCookie(response, Constants.USER_LOGIN, user.getId()+""
						, 60*60, appconfig.getDomain(), "/");
				
				ret.put("status", Constant.API_STATUS_SUCCESS);
				ret.put("message", "登陆成功");
			}else {
				ret.put("status", Constant.API_STATUS_FAILED);
				ret.put("message", "用户名或密码错误");
			}
		}else {
			ret.put("status", Constant.API_STATUS_FAILED);
			ret.put("message", "请填写用户名或密码");
		}
		
		log.info("user login response:{}",ret.toJSONString());
		return new ResponseEntity<String>(ret.toJSONString(), headers, HttpStatus.OK);
	}
	
	
	@ApiOperation(value="导入账号数据", notes="")
	@RequestMapping(method={RequestMethod.POST},path="/importExc")
	public @ResponseBody ResponseEntity<String> importExc(
			@ApiParam(value = "待上传的文件") @RequestParam(name = "file") MultipartFile file) {
		HttpHeaders headers = new HttpHeaders();
		headers.put(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json;charset=UTF-8"));
		JSONObject ret = new JSONObject();
		int linenum = 0;

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
			String line = "";
			int r = -1;
			int num = 0;
			
			while ((line = reader.readLine()) != null) {
				log.info(line);
				num++;
				String[] arrays = line.split(",");
				if (arrays.length == 4) {
//					String _v1 = new String(arrays[0].getBytes("UTF-8"),"gb2312"); // 客户名称
					String v1 = arrays[0].trim(); // 公司名称
					String v2 = arrays[1].trim(); // 产品名称
					String v3 = arrays[2].trim(); // 账户名称
					String v4 = arrays[3].trim(); // 密码

					User user = new User();
					user.setCompany(v1);
//					user.setProduct(v2);
					user.setPhone("132");
					user.setAddress("朝阳");
					user.setName(v3num+""+num+""+num+"@qq.com");
					user.setPwd(EncryptUtil.encrypt(v4"123456"));
					user.setUserType(0);
					user.setOperateType(0);
					user.setReviewState(1);
					user.setUsable(true);
					
					r = createUser(user);
				}
				
				if (r >= 0) {
					linenum++;
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("总计:" + linenum);

		return new ResponseEntity<String>(ret.toJSONString(), headers, HttpStatus.OK);
	}
	
	private int createUser(User user) {
		try {
			this.userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	
}
/*
 *public class CookieUtil {
   private static final Logger log = LoggerFactory.getLogger(CookieUtil.class);

   public static void addCookie(HttpServletRequest request, HttpServletResponse response, String key, String value, int maxage) {
      Cookie _cookie = new Cookie(key, value);
      _cookie.setMaxAge(maxage);
      _cookie.setPath("/");
      String rootDomain = request.getServerName().replaceAll(".*\\.(?=.*\\.)", "");
      _cookie.setDomain(rootDomain);
      log.info("add  cookie key:{} value:{}", key, value);
      response.addCookie(_cookie);
   }

   public static void addCookie(HttpServletResponse response, String key, String value, int maxage, String domain, String path) {
      Cookie _cookie = new Cookie(key, value);
      _cookie.setMaxAge(maxage);
      _cookie.setPath(path);
      _cookie.setDomain(domain);
      log.info("add  cookie key:{} value:{}", key, value);
      response.addCookie(_cookie);
   }

   public static String getCookie(HttpServletRequest request, String key) {
      try {
         Cookie[] e = request.getCookies();
         if(e != null) {
            Cookie[] arg2 = e;
            int arg3 = e.length;

            for(int arg4 = 0; arg4 < arg3; ++arg4) {
               Cookie _cookie = arg2[arg4];
               if(_cookie.getName().equalsIgnoreCase(key)) {
                  log.info("get cookie name:{} value:{}", key, _cookie.getValue());
                  return _cookie.getValue();
               }
            }
         }
      } catch (Exception arg6) {
         arg6.printStackTrace();
      }

      log.info("not found cookie for name:{} ", key);
      return null;
   }

   public static void main(String[] args) {
   }
} 
 */
